//引入express模块
const express = require("express");
//创建用户路由器对象
const router = express.Router();
//引入数据库连接池对象
const userPool = require("../pool.js");

//1.添加用户注册路由(post /register)
router.post('/register',(req,res,next)=>{
	//1.1接受流传递的数据
	console.log(req.body);
	let obj = req.body;
	//1.2判断输入的元素是否为空
	let n = 400;
	for(let k in obj){ 
		if(!obj[k]){
			//若添加的元素为空，报错跳出
			res.send({"code":n++,"msg":k+"不能为空"});
			return;
		}
	}
	//1.3执行SQL命令
	userPool.query('INSERT INTO aa_user SET ?',[obj],(err,result)=>{
		//判断是否出错
		if(err){
			//将错误保存，传递给错误处理中间件
			next(err);
			//结束执行
			return;
		}	
		//打印result，判断affectedRows是否位0，为0则注册失败，否则注册成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"注册失败"});
		}else{
			res.send({"code":200,"msg":"注册成功"});
		}
	});
});

//2.添加用户登录路由(post /login)
router.post('/login',(req,res,next)=>{
	//2.1接受流传递的数据
	console.log(req.body);
	let obj = req.body;
	//2.2执行SQL命令
	userPool.query('SELECT * FROM aa_user WHERE uname = ? AND upwd = ?',[obj.uname,obj.upwd],(err,result)=>{
		if(err){
			//将错误保存并传递给错误处理中间件
			next(err);
			//结束执行
			return;
		}
		//在后台打印结果，并判断result.length是否大于0
		console.log(result);
		if(result.length>0){
			res.send({"code":200,"msg":"登录成功",'data':result});
		}else{
			res.send({"code":201,"msg":"登录失败",'data':result});
		}
	});
});

//3.添加用户修改路由(put /)
router.put('/',(req,res,next)=>{
	//3.1获取路由传参的数据
	console.log(req.body);
	let obj = req.body;
	//3.2判断用户修改的值是否为空，为空则跳出并报错
	let n=400;
	for(let k in obj){
		if(!obj[k]){
			res.send({"code":n++,"msg":k+"不能为空"});
			return;
		}
	}
	//3.3执行SQL命令
	userPool.query('UPDATE aa_user SET ? WHERE uid=?',[obj,obj.uid],(err,result)=>{
		if(err){
			//出错时将错误传递给错误处理中间件
			next(err);
			return;
		}
		//判断是否修改成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"修改失败"});
		}else{
			res.send({"code":200,"msg":"修改成功"});
		}
	});
});

//4.添加用户删除路由(delete /编号)
router.delete('/:uid',(req,res,next)=>{
	//4.1接受流传递的数据
	console.log(req.params);
	let obj = req.params;
	//4.2执行SQL命令
	userPool.query('DELETE FROM aa_user WHERE uid=?',[obj.uid],(err,result)=>{
		if(err){
			//出错时将错误传递给错误处理中间件
			next(err);
			return;
		}
		//判断是否删除成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"删除失败"});
		}else{
			res.send({"code":200,"msg":"删除成功"});
		}	
	});
});

//5.添加用户列表查询路由(get /)
router.get('/',(req,res,next)=>{
	//5.1获取查询字符串传递的数据
	console.log(req.query);
	let obj = req.query;
	//5.2判断是否为空
	if(!obj.pno) obj.pno = 1;
	if(!obj.count) obj.count = 5;
	//5.3计算开始查询的值
	let start = (obj.pno-1)*obj.count;
	//5.4将每页数据量装为整形
	let size = parseInt(obj.count);
	//执行SQL命令
	userPool.query('SELECT * FROM aa_user LIMIT ?,?',[start,size],(err,result)=>{
		if(err){
			//出错时将错误传递给错误处理中间件
			next(err);
			return;
		}
		//判断是否删除成功
		console.log(result);
		if(result.length>0){
			res.send({"code":200,"msg":"查询成功","data":result});
		}else{
			res.send({"code":201,"msg":"查询失败","data":result});
		}		
	});
});

//6.添加用户查询路由(get /get)
router.get('/get',(req,res,next)=>{
	//6.1获取查询字符串传递的数据
	console.log(req.query);
	let obj = req.query;
	//6.2执行SQL命令
	userPool.query('SELECT * FROM aa_user WHERE ?',[obj],(err,result)=>{
		if(err){
			//出现错误，将错误保存传递给错误处理中间件
			next(err);
			return;
		}
		//通过affectedRows是否等于零判断查询是否成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"查询失败","data":result});
		}else{
			res.send({"code":200,"msg":"查询成功","data":result});
		}
	});
});

//7.添加获取所有用户信息(get /userlist)
router.get('/userlist',(req,res,next)=>{
	//获取查询字符串传递的数据
	console.log(req.query);
	//执行SQL命令
	userPool.query('SELECT * FROM aa_user',(err,result)=>{
		if(err){
			//出现错误，将错误保存并传递给错误处理中间件
			next(err);
			return;
		}
		//判断查询是否成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"查询失败","data":result});
		}else{
			res.send({"code":200,"msg":"查询成功","data":result});
		}
	});
});

//导出用户路由器对象
module.exports = router;